// pages/wenzhangye/wenzhangye.js
Page({
  _hanlerCommentReady: function () {
    //获取评论组件实例，调用内部获取评论的方法
    let c = this.selectComponent("#comments");
    console.log('调用')
    c._getComments(this.data.pageid)
  },
  onEditorReady() {
    const that = this
    wx.createSelectorQuery().select('#editor').context(function (res) {
      that.editorCtx = res.context
    }).exec()
  },
  _upLoadTime: function(){//上传时间
    let total_time = Date.now() - this.data.start_time
    let collection_name = ''
    let userid = ''
    wx.cloud.database().collection(collection_name).add({
      userid: userid,
      total_time: total_time,
      resourceid: this.data.pageid
    }).catch(err => {
      console.log(err)
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    title: "长长长长长长长长长长长标题",
    editor: "作者",
    content_html: '',
    content_delta: {},
    time: "时间",
    pageid: "",
    windowHeight: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('op=', options)
    this.data.pageid = options.id || ''
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          windowHeight: res.windowHeight - 130
        })
      },
      fail: (err) => {
        console.log(err)
      }
    })
    this.data.start_time = Date.now()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this
    if(this.data.pageid == ''){return}
    wx.cloud.database().collection('resources').where({
      _id: this.data.pageid
    }).field({
      editor: true, title: true, date: true, content_html: true, content_delta: true
    }).get().then(res => {
      console.log(res)
      let time = res.data[0].date
      if(time){
        time = "" + time.getFullYear().toString() + "-" + (time.getMonth() + 1).toString() + "-" + time.getDate().toString() + ' ' + time.getHours().toString() + ":" + time.getMinutes().toString()
      }else{
        time = 'unknown'
      }
      that.editorCtx.setContents({
        html: res.data[0]['content_html'] || '<p>ERROR，文章id出错</p>',
        delta: res.data[0]['content_delta'],
        fail: () => {
          that.editorCtx.setContents({html: '<p>ERROR</p>'})
        }
      })
      this.setData({
        title: res.data[0].title,
        editor: res.data[0].editor,
        time: time,
      })
    }).catch(err => {
      that.editorCtx.setContents({
        html: '<p>无法获取文章内容</p>',
        fail: () => {
          that.editorCtx.setContents({html: '<p>ERROR</p>'})
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})